<?php
    list($usec, $sec)   =   explode(' ', microtime());
    define('BEGIN_TIME', ((float)$usec + (float)$sec));
    error_reporting(E_ALL);
    require 'config.php';
    require 'var.php';
    require 'core/function.php';
    function _qrcode($str, $mode = 'png', $level = null, $size = 4, $margin = 4, $saveandprint = false)
    {
        sfget_instance('Class_Qrcode_qrlib');
        if ($level === null) {
            $level = QR_ECLEVEL_L;
        }
        switch ($mode) {
            case 'png':
                QRcode::png($str, false, $level, $size, $margin, $saveandprint);
                break;
            case 'url':
            case 'file':
                $key = md5($str);
                $path = '/asset/cache/file/qrcode/' . get_md5_dir($key) . '/' . $key . '.png';
                $file = ROOT_DIR . $path;
                if (!is_file($file)) {
                    $dir = dirname($file);
                    if (!is_dir($dir)) {
                        mkdir($dir, 0777, true);
                    }
                    QRcode::png($str, $file, $level, $size, $margin, $saveandprint);
                }
                if ($mode == 'url') {
                    return SITEURL_ROOT . $path;
                } else {
                    return $file;
                }
        }
    }

    function _barcode($str, $size = 1, $font_size = 8, $code = 'BCGcode128', $font_family = 'Arial.ttf', $filetype = 'PNG', $dpi = 72, $rotation = null)
    {
        $image = sfget_instance('Class_Barcodegen_image');
        $image->finish($str, $size, $font_size, $code, $font_family, $filetype, $dpi, $rotation);
    }
    ob_start();
    if (!empty($_GET['barcode']) && (count($_GET) == 1 || count($_GET) == 2 && isset($_GET['openid']) || count($_GET) == 2 && isset($_GET['code']) || count($_GET) == 2 && isset($_GET['size']) || count($_GET) == 3 && isset($_GET['size']))) {
        $size = 8;
        if (!empty($_GET['size'])) {
            $size = $_GET['size'];
        }
        if (!empty($_GET['code'])) {
            _barcode($_GET['barcode'], $size, null, $_GET['code']);
        } else {
            _barcode($_GET['barcode'], $size, null);
        }
        sfquit();
    }
    if ( !empty($_GET['qrcode']) && (count($_GET) == 1 || count($_GET) == 2 && isset($_GET['openid']) || count($_GET) == 2 && isset($_GET['size'])) ) {
        if (isset($_GET['size'])) {
            _qrcode($_GET['qrcode'], 'png', null, $_GET['size']);
        } else {
            _qrcode($_GET['qrcode']);
        }
        sfquit();
    }
    sfquit();